package cn.dujc.superkid.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.superkid.R;

import cn.dujc.superkid.bean.VideoListResult;

public class ExerciseAdapter extends RecyclerView.Adapter<ExerciseAdapter.ViewHolder> {
    private List<VideoListResult.ExercisesEntity> list;
    private Context context;
    public List<VideoListResult.ExercisesEntity> getList() {
        return list;
    }

    public void setList(List<VideoListResult.ExercisesEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    private UnitTestAdapter.OnItemViewClickListener onItemViewClickListener;

    public ExerciseAdapter(Context context) {
        this.context = context;
    }

    public void setOnItemViewClickListener(UnitTestAdapter.OnItemViewClickListener onItemViewClickListener) {
        this.onItemViewClickListener = onItemViewClickListener;
    }

    @Override
    public ExerciseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_exercises, null);
        return new ExerciseAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ExerciseAdapter.ViewHolder holder, final int position) {
        final VideoListResult.ExercisesEntity bean = list.get(position);
        if (list.get(position).getExercises_answer_type() .equals("3")) {
            holder.exercise_tiankong.loadData(list.get(position).getExercises_title(),"text/html","utf-8");
            holder.select.setVisibility(View.GONE);
            holder.selects.setVisibility(View.GONE);
            holder.fill_in_blank.setVisibility(View.VISIBLE);
            holder.excercise_answer.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!TextUtils.isEmpty(holder.excercise_answer.getText())) {
                        bean.setWrite(true);
                        bean.setValue("\""+holder.excercise_answer.getText()+"\"");
                    }
                }
            });

        } else if (list.get(position).getExercises_answer_type() .equals("1")) {
            holder.exercise_select.loadData(list.get(position).getExercises_title(),"text/html","utf-8");
            holder.fill_in_blank.setVisibility(View.GONE);
            holder.selects.setVisibility(View.GONE);
            holder.select.setVisibility(View.VISIBLE);
            holder.duoxuan.loadData(list.get(position).getExercises_title(),"text/html","utf-8");
            holder.radiobutton1.setText("A " + Html.fromHtml(list.get(position).getExercises_answer().get(0)));
            holder.radiobutton2.setText("B " + Html.fromHtml(list.get(position).getExercises_answer().get(1)));
            holder.radiobutton3.setText("C " + Html.fromHtml(list.get(position).getExercises_answer().get(2)));
            holder.radiobutton4.setText("D " + Html.fromHtml(list.get(position).getExercises_answer().get(3)));
            CompoundButton.OnCheckedChangeListener l = new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        if (holder.radiobutton1 == buttonView) {
                            bean.setWrite(true);
                            bean.setValue("[\"1\"]");
                        }
                        if (holder.radiobutton2 == buttonView) {
                            bean.setWrite(true);
                            bean.setValue("[\"2\"]");
                        }
                        if (holder.radiobutton3 == buttonView) {
                            bean.setWrite(true);
                            bean.setValue("[\"3\"]");
                        }
                        if (holder.radiobutton4 == buttonView) {
                            bean.setWrite(true);
                            bean.setValue("[\"4\"]");
                        }
                    }
                }
            };
            holder.radiobutton1.setOnCheckedChangeListener(l);
            holder.radiobutton2.setOnCheckedChangeListener(l);
            holder.radiobutton3.setOnCheckedChangeListener(l);
            holder.radiobutton4.setOnCheckedChangeListener(l);


        } else if (list.get(position).getExercises_answer_type() .equals("2")) {
            holder.fill_in_blank.setVisibility(View.GONE);
            holder.selects.setVisibility(View.VISIBLE);
            holder.select.setVisibility(View.GONE);
            holder.duoxuan.loadData(list.get(position).getExercises_title(),"text/html","utf-8");

            holder.checkbox1.setText("A " + Html.fromHtml(list.get(position).getExercises_answer().get(0)));
            holder.checkbox2.setText("B " + Html.fromHtml(list.get(position).getExercises_answer().get(1)));
            holder.checkbox3.setText("C " + Html.fromHtml(list.get(position).getExercises_answer().get(2)));
            holder.checkbox4.setText("D " + Html.fromHtml(list.get(position).getExercises_answer().get(3)));
            final List<CheckBox> boxs = new ArrayList<>();
            boxs.add(holder.checkbox1);
            boxs.add(holder.checkbox2);
            boxs.add(holder.checkbox3);
            boxs.add(holder.checkbox4);


            CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    StringBuffer sb = new StringBuffer("");
                    for (int i = 0; i < boxs.size(); i++) {
                        if (boxs.get(i).isChecked()) {
                            sb.append(",\"" + (i + 1)+"\"");
                            bean.setWrite(true);
                        }
                    }
                    if (sb.toString().equals("")) {
                        bean.setWrite(false);
                        bean.setValue("");
                    }else{
                        bean.setValue("["+sb.toString().substring(1)+"]");
                    }
                }
            };
            holder.checkbox1.setOnCheckedChangeListener(listener);
            holder.checkbox2.setOnCheckedChangeListener(listener);
            holder.checkbox3.setOnCheckedChangeListener(listener);
            holder.checkbox4.setOnCheckedChangeListener(listener);

        }
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemViewClickListener.onItemViewClick(v, position);
            }
        };
        holder.jxView.setOnClickListener(listener);
        holder.addGood.setOnClickListener(listener);
    }

    public String getValue() {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < list.size(); i++) {
            sb.append(",\"answer" + (i + 1) + "\":" + list.get(i).getValue());

        }
        return "{" + sb.toString().substring(1) + "}";
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private View mView;
        private WebView exercise_tiankong;
        private LinearLayout fill_in_blank;
        private LinearLayout select;
        private LinearLayout selects;
        private WebView exercise_select;
        private WebView duoxuan;
        private CheckBox checkbox1;
        private CheckBox checkbox2;
        private CheckBox checkbox3;
        private CheckBox checkbox4;
        private RadioButton radiobutton1;
        private RadioButton radiobutton2;
        private RadioButton radiobutton3;
        private RadioButton radiobutton4;
        private EditText excercise_answer;
        private TextView jxView;
        private TextView addGood;

        public ViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            addGood = itemView.findViewById(R.id.item_exercises_add);
            fill_in_blank = itemView.findViewById(R.id.tiankong);
            select = itemView.findViewById(R.id.danxuan);
            selects = itemView.findViewById(R.id.duoxuan);
            exercise_tiankong = itemView.findViewById(R.id.exercise_descrp);
            exercise_select = itemView.findViewById(R.id.exercise_select);
            duoxuan = itemView.findViewById(R.id.exercise_selects);
            excercise_answer = itemView.findViewById(R.id.excercise_answer);
            radiobutton1 = itemView.findViewById(R.id.radiobutton1);
            radiobutton2 = itemView.findViewById(R.id.radiobutton2);
            radiobutton3 = itemView.findViewById(R.id.radiobutton3);
            radiobutton4 = itemView.findViewById(R.id.radiobutton4);
            checkbox1 = itemView.findViewById(R.id.checkbox1);
            checkbox2 = itemView.findViewById(R.id.checkbox2);
            checkbox3 = itemView.findViewById(R.id.checkbox3);
            checkbox4 = itemView.findViewById(R.id.checkbox4);
            jxView = itemView.findViewById(R.id.exercises_jiexi);
            jxView.setVisibility(View.GONE);
            addGood.setVisibility(View.GONE);
        }
    }

    public interface OnItemViewClickListener {
        void onItemViewClick(View v, int pos);
    }
}